import {
  INCLUDE_TAG,
  EXCLUDE_TAG,
  END_TAG,
  SUBPACKAGE_PAGE,
  TAB_PAGE,
  HOME_PAGE,
  hasOwn,
  clone,
  PAGE_CONFIG,
  PAGE_USING,
  PROFILE_DIR,
  logWarn
} from '@shared';
import {
  serializePlatforms,
  findTabBarIndex,
  findSubpackageIndex,
  normalizeRootPath
} from '@core/util';
// 重组 routers
export function groupRouters(routers, tabBar) {
  var pages = [],
    subpackages = [],
    i = 0,
    size = routers.length,
    element = null;

  for (; i < size; i++) {
    element = routers[i];
    // 首先移除注释信息
    if (hasOwn(element, PAGE_CONFIG)) {
      const comment = element[PAGE_CONFIG];
      if (hasOwn(comment, PAGE_USING) && !comment[PAGE_USING]) {
        continue;
      } else {
        delete element[PAGE_CONFIG];
      }
    }
    // platforms
    if (hasOwn(element, INCLUDE_TAG)) {
      element[INCLUDE_TAG] = serializePlatforms(element[INCLUDE_TAG]);
    } else if (hasOwn(element, EXCLUDE_TAG)) {
      element[EXCLUDE_TAG] = serializePlatforms(element[EXCLUDE_TAG]);
    }
    // tabBar
    if (hasOwn(element, TAB_PAGE)) {
      element[TAB_PAGE].pagePath = element.path;
      if (tabBar) {
        const tabBarIndex = findTabBarIndex(element.path, tabBar.list);
        if (~tabBarIndex) {
          tabBar.list[tabBarIndex] = clone(element[TAB_PAGE]);
          delete element[TAB_PAGE];
        } else {
          logWarn(
            `检测到未在 ${PROFILE_DIR}/tab-bar.json 中配置的选项卡页面 ${element.path} 请及时处理`
          );
        }
      }
    }
    if (hasOwn(element, HOME_PAGE)) {
      // home
      delete element[HOME_PAGE];
      pages.unshift(element);
    } else if (hasOwn(element, SUBPACKAGE_PAGE)) {
      // subpackages
      subpackages.push(element);
    } else {
      // pages
      pages.push(element);
    }
  }

  return {
    pages,
    subpackages,
    tabBar
  };
}
// subpackages
export function groupSubpackages(routers) {
  const subpackages = [];
  let i = 0,
    size = routers.length,
    element = null;

  for (; i < size; i++) {
    element = routers[i];
    const pageRoot = normalizeRootPath(element[SUBPACKAGE_PAGE].root);
    const subpackageIndex = findSubpackageIndex(pageRoot, subpackages);
    // handle
    delete element[SUBPACKAGE_PAGE];
    element.path = element.path.replace(pageRoot, '');
    if (~subpackageIndex) {
      subpackages[subpackageIndex].pages.push(element);
    } else {
      subpackages.push({
        root: pageRoot,
        pages: [element]
      });
    }
  }

  return subpackages;
}
// 重排 routers
export function orderRoutersByPlatforms(routers) {
  var appRouters = [],
    includePlatforms = {},
    excludePlatforms = {},
    includeValue = '',
    excludeValue = '',
    includeRouters = [],
    excludeRouters = [];

  var i = 0,
    size = routers.length,
    router;

  for (; i < size; i++) {
    router = routers[i];
    if (hasOwn(router, INCLUDE_TAG)) {
      includeValue = router[INCLUDE_TAG];
      delete router[INCLUDE_TAG];

      if (!hasOwn(includePlatforms, includeValue)) {
        includePlatforms[includeValue] = [
          {
            [INCLUDE_TAG]: includeValue
          }
        ];
      }
      includePlatforms[includeValue].push(router);
    } else if (hasOwn(router, EXCLUDE_TAG)) {
      excludeValue = router[EXCLUDE_TAG];
      delete router[EXCLUDE_TAG];

      if (!hasOwn(excludePlatforms, excludeValue)) {
        excludePlatforms[excludeValue] = [
          {
            [EXCLUDE_TAG]: excludeValue
          }
        ];
      }
      excludePlatforms[excludeValue].push(router);
    } else {
      appRouters.push(router);
    }
  }
  // 合并数据
  for (let includeKey in includePlatforms) {
    includeRouters = includeRouters.concat(includePlatforms[includeKey], {
      [END_TAG]: true
    });
  }
  for (let excludeKey in excludePlatforms) {
    excludeRouters = excludeRouters.concat(excludePlatforms[excludeKey], {
      [END_TAG]: true
    });
  }

  return appRouters.concat(includeRouters, excludeRouters);
}
